package com.zj198.action.common;

import java.util.List;

import com.zj198.action.BaseAction;
import com.zj198.model.DicCity;
import com.zj198.model.DicCommon;
import com.zj198.model.DicDistrict;
import com.zj198.model.DicProvince;
import com.zj198.service.common.DictoryDataService;

public class CommonDataAction extends BaseAction{
	private Integer provinceId;
	private Integer cityId;
	private List<DicProvince> listProvince;//省份
    private List<DicCity> listCity;//省份下的城市
    private List<DicDistrict> listDistrict;//城市下的区
    
	private Integer id;//唯一的id值
	private String ids;//多个用户已选中的id值，应该是"，"号隔开的
	private Integer groupId;//组id
	private String css;//html标签对应的class名称
	private String name;//html标签对应的name名
	private String idName;//html对应的id的名称
    private DictoryDataService dictoryDataService;
    
	public String findProvince() {
		listProvince = dictoryDataService.findAllProvince();
		return "provinceList";
	}
	
	public String findCity(){
		listCity = dictoryDataService.findCityByProvinceid(provinceId);
		return "cityList";
	}

	public String findDistrict(){
		listDistrict = dictoryDataService.findDistrictByCityid(cityId);
		return "districtList";
	}
	
	public String execute(){
		msg = dictoryDataService.getValueNameById(id);
		return "ajax";
	}
	
	public String dataSelect() {
		StringBuffer str = new StringBuffer();
		List<DicCommon> list = dictoryDataService.findCommonDataByGroupId(groupId);
		str.append("<select id='").append(idName).append("' name='").append(name).append("' class='").append(css).append("'>");
		for(DicCommon c:list){
			str.append("<option value='").append(c.getId()).append("'");
			if(c.getId()-id==0){
				str.append(" selected='selected'");
			}
			str.append(">").append(c.getName()).append("</option>");
		}
		msg = str.append("</select>").toString();
		return "ajax";
	}

	public String dataCheckbox() {
		StringBuffer str = new StringBuffer();
		List<DicCommon> list = dictoryDataService.findCommonDataByGroupId(groupId);
		String[] idss = ids.split(",");
		for(DicCommon c:list){
			str.append("<input type='checkbox' name='").append(name).append("' class='").append(css).append("' value='").append(c.getId()).append("'");
			for(String s:idss){
				if(s.length()==0){
					break;
				}
				if(Integer.parseInt(s)-c.getId()==0){
					str.append(" checked='checked'");
					break;
				}
			}
			str.append("<label for='").append(idName).append("'>").append(c.getName()).append("</label>");
		}
		msg = str.toString();
		return "ajax";
	}

	public String dataRadiobox() {
		StringBuffer str = new StringBuffer();
		List<DicCommon> list = dictoryDataService.findCommonDataByGroupId(groupId);
		for(DicCommon c:list){
			str.append("<input type='radio' class='").append(css).append("' name='").append(name).append("' value='").append(c.getId()).append("'");
			if(c.getId()-id==0){
				str.append(" checked='checked'");
			}
			str.append("/><label for='").append(name).append("'>").append(c.getName()).append("</label>");
		}
		msg = str.toString();
		return "ajax";
	}
	
	//setter and getter
	public List<DicCity> getListCity() {
		return listCity;
	}
	public void setProvinceId(Integer provinceId) {
		this.provinceId = provinceId;
	}
	public void setCityId(Integer cityId) {
		this.cityId = cityId;
	}
	public List<DicProvince> getListProvince() {
		return listProvince;
	}
	public List<DicDistrict> getListDistrict() {
		return listDistrict;
	}
	public void setDictoryDataService(DictoryDataService dictoryDataService) {
		this.dictoryDataService = dictoryDataService;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public void setGroupId(Integer groupId) {
		this.groupId = groupId;
	}
	public void setCss(String css) {
		this.css = css;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setIdName(String idName) {
		this.idName = idName;
	}

}
